package com.fg.pro.ai.AiController;


import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.memory.redis.RedisChatMemoryRepository;
import com.fg.pro.model.reponse.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "aiChat_redis_memory",description = "AiChat_Redis_Memory")
@RestController
@RequestMapping("aiChat_redis_memory")
public class AiChat_Redis_Memory {

    @Autowired
    DashScopeChatModel dashScopeChatModel;

    @Autowired
    private ChatMemory chatMemory;

    @Operation(summary = "默认回答（拦截器自动加入redis记忆体-不同用户）",description = "")
    @GetMapping("/chartByAdviceAndMemoryAndUser")
    public ResponseResult chartByAdviceAndMemoryAndUser(@RequestParam(required = false)String question){

        ChatClient client = ChatClient
                .builder(dashScopeChatModel)
                .defaultAdvisors(
                        PromptChatMemoryAdvisor.builder(chatMemory)
                                .conversationId("123123") // 在builder中设置
                                .build()
                )
                .build();
        String content = client
                .prompt()
                .user(question)
                .call()
                .content();
        return ResponseResult.okResult(content);
    }

//    此配置，当前类生效
//    只能在test测试类中生效
//    @Configuration
//    static class Config{
//
//    @Value("${spring.data.redis.host}")
//    private String host;
//
//    @Value("${spring.data.redis.port}")
//    private Integer port;
//
//    @Value("${spring.data.redis.password}")
//    private String password;
//
//    @Bean
//    public RedisChatMemoryRepository redisChatMemoryRepository() {
//        return RedisChatMemoryRepository
//                .builder()
//                .host(host)
//                .port(port)
//                .password(password)
//                .timeout(666666)
//                .build();
//    }
//
////        配置记忆体
//        @Bean
//        ChatMemory chatMemory(RedisChatMemoryRepository chatMemoryRepository){
//            return MessageWindowChatMemory
//                    .builder()
//                    .maxMessages(1)
//                    .chatMemoryRepository(chatMemoryRepository)
//                    .build();
//        }
//    }



}
